import { mobileLogin,getUserinfo,getUsers,setCookie,removeCookie,getCookie } from '@/api/login'

import { getcitylist,getregionlist } from '@/api/homepage'

export default {
    loginByAccount({commit},loginRuleForm){
        mobileLogin(loginRuleForm).then((res)=>{
            if(res.data.code == 0){
                const data = res.data.data;
                commit('FETCH_TOKEN',data.token);
                commit('LOGIN_STATUS',true);
                commit('USER_TYPE',data.user_type);
                setCookie('wangyiaccid',data.wang_yi_accid,86400000);
                setCookie('wangyitoken',data.wang_yi_token,86400000);
            }
        })
        .catch((error)=>{
        })
    },
    
    loginByCode({commit},loginByCodeForm){
        mobileLogin(loginByCodeForm).then((res)=>{
            if(res.data.code == 0){
                let data = res.data.data;
                commit('FETCH_TOKEN',data.token);
                commit('LOGIN_STATUS',true);
                commit('USER_TYPE',data.user_type);
                setCookie('wangyiaccid',data.wang_yi_accid);
                setCookie('wangyitoken',data.wang_yi_token);
                this.$message.success('登录成功！')
            }else {
                this.$message.error(res.data.msg)
            }
        })
        .catch(error=>{
        })
    },
    
    fetchUserInfo({commit},tokens){
        getUserinfo(tokens).then((res)=>{
            if(res.data.code == 0){
                let data = res.data.data;
                commit('GET_USERNAME',data.user_name);
                commit('GET_USERPIC',data.face_url);
            }
        })
        .catch((error)=>{
    
        })
    },

    fetchUsers({commit},tokens){
        getUsers(tokens).then((res)=>{
            if(res.data.code == 0){
                let data = res.data.data;
                commit('GET_USERNAME',data.user_name);
                commit('GET_USERPIC',data.face_url);
            }
        })
        .catch((error)=>{
    
        })
    },

    fetchCitylist({commit}){
        getcitylist().then((res)=>{
            if(res.data.code == 0){
                let citydata = res.data.data;
                commit('GET_CITY',citydata.user_name);
            }
        })
        .catch((error)=>{
            
        })
    },

    fetchRegionlist({commit}){
        getregionlist().then((res)=>{
            if(res.data.code == 0){
                let regiondata = res.data.data;
                commit('GET_REGION',regiondata.user_name);
            }
        })
        .catch((error)=>{
    
        })
    },

    GetSearch({commit},sharesobj){
        commit('GET_SHARE_SELECT',sharesobj);
    },
    ResetSelect({commit},sharesobj){
        commit('RESET_SELECT',sharesobj);
    },

    logOut({ state }) {
        state.username = '';
        state.token = '';
        state.userpic = '';
        state.islogin = false;
        window.localStorage.removeItem('username');
        window.localStorage.removeItem('token');
        window.localStorage.removeItem('user_type');
        window.localStorage.removeItem('islogin');
        removeCookie('sunzheng');
        removeCookie('sunzhengtype');
        removeCookie('wangyiaccid');
        removeCookie('wangyitoken');
        removeCookie('userpic');
        removeCookie('username');
        setCookie('islogin',false);
    }
}

